package net.adst.beans;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 05.11.12
 * Time: 9:57
 * To change this template use File | Settings | File Templates.
 */

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.util.HSSFColor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OurTecRow {
    private Date date;
    private String chIN;
    private String chOut;
    private String sureName;
    private String hoursFacts;
    private String hours;
    private String priceForHour;
    private String profit;
    private String kindOfPayment;
    private String customer;
    private String objectName;
    private String dtAmount;
    private String dtCost;
    private String salary;
    private String dopTraty;
    private String opisanie;
    private String dohod;
    private String arenda;
    private String prorab;
    private String comment;

    private Boolean paidFor;
    private String shortName;
    private int rowInFile;

    public OurTecRow(HSSFRow row, String shortName) {
        this.shortName = shortName;
        if (row.getCell(0).getCellStyle().getFillForegroundColor() == HSSFColor.YELLOW.index) {
            paidFor = true;
        } else {
            paidFor = false;
        }


        if (row.getCell(0).toString() != null) {
            this.date = row.getCell(0).getDateCellValue();
        } else {
            this.date = new Date();
        }
        if (row.getCell(1).toString() != null) {
            this.chIN = row.getCell(1).toString();
        } else {
            this.chIN = "";
        }
        if (row.getCell(2).toString() != null) {
            this.chOut = row.getCell(2).toString();
        } else {
            this.chOut = "";
        }

        if (row.getCell(3).toString() != null) {
            this.sureName = row.getCell(3).toString();
        } else {
            this.sureName = "";
        }
        if (row.getCell(4).toString() != null) {
            this.hoursFacts = row.getCell(4).toString();
        } else {
            this.hoursFacts = "";
        }
        if (row.getCell(5).toString() != null) {
            this.hours = row.getCell(5).toString();
        } else {
            this.hours = "";
        }

        if (row.getCell(6).toString() != null) {
            this.priceForHour = row.getCell(6).toString();
        } else {
            this.priceForHour = "";
        }

        if (row.getCell(7).toString() != null && !priceForHour.isEmpty() && !hours.isEmpty()) {
            this.profit = String.valueOf(Double.valueOf(priceForHour) * Double.valueOf(hours));
        } else {
            this.profit = "";
        }


        if (row.getCell(8).toString() != null) {
            this.kindOfPayment = row.getCell(8).toString();
        } else {
            this.kindOfPayment = "";
        }

        if (row.getCell(9).toString() != null) {
            this.customer = row.getCell(9).toString();
        } else {
            this.customer = "";
        }

        if (row.getCell(10).toString() != null) {
            this.objectName = row.getCell(10).toString();
        } else {
            this.objectName = "";
        }

        if (row.getCell(11).toString() != null) {
            this.dtAmount = row.getCell(11).toString();
        } else {
            this.dtAmount = "";
        }

        if (row.getCell(12).toString() != null) {
            this.dtCost = row.getCell(12).toString();
        } else {
            this.dtCost = "";
        }

        if (row.getCell(13).toString() != null) {
            this.salary = row.getCell(13).toString();
        } else {
            this.salary = "";
        }

        if (row.getCell(14).toString() != null) {
            this.dopTraty = row.getCell(14).toString();
        } else {
            this.dopTraty = "";
        }

        if (row.getCell(15) != null && row.getCell(15).toString() != null) {
            this.opisanie = row.getCell(15).toString();
        } else {
            this.opisanie = "";
        }

        if (row.getCell(17).toString() != null) {
            this.arenda = row.getCell(17).toString();
        } else {
            this.arenda = "";
        }

        if (row.getCell(18).toString() != null) {
            this.prorab = row.getCell(18).toString();
        } else {
            this.prorab = "";
        }

        if ((row.getCell(16).toString() != null)) {

            Double profit = this.profit.isEmpty() ? 0 : Double.valueOf(this.profit);
            Double dtCost = this.dtCost.isEmpty() ? 0 : Double.valueOf(this.dtCost);
            Double salaryInRub = this.salary.isEmpty() ? 0 : Double.valueOf(this.salary);
            Double dopTraty = this.dopTraty.isEmpty() ? 0 : Double.valueOf(this.dopTraty);
            Double arenda = this.arenda.isEmpty() ? 0 : Double.valueOf(this.arenda);
            Double prorab = this.prorab.isEmpty() ? 0 : Double.valueOf(this.prorab);
            double nal = 1.0;
            this.dohod = String.valueOf(profit * nal - dtCost - salaryInRub - dopTraty - arenda - prorab);
        } else {
            this.dohod = "";
        }

        if (row.getCell(19) != null || row.getCell(20) != null || row.getCell(21) != null || row.getCell(22) != null || row.getCell(23) != null || row.getCell(24) != null || row.getCell(25) != null || row.getCell(26) != null || row.getCell(27) != null) {
            this.comment = row.getCell(19) != null ? row.getCell(19).toString() : "";
            for (int i = 20; i < 27; i++) {
                if (row.getCell(i) != null && !row.getCell(i).toString().equals("")) {
                    this.comment = this.comment.equals("") ? row.getCell(i).toString() : this.comment + "\n" + row.getCell(i).toString();
                }
            }
            if (this.comment == null) {
                this.comment = "";
            }
        } else {
            this.comment = "";
        }

        this.rowInFile = row.getRowNum();
    }

    public OurTecRow(Date date, String chIN, String chOut, String sureName, String hoursFacts, String hours, String priceForHour, String profit, String kindOfPayment, String customer, String objectName, String dtAmount, String dtCost, String salary, String dopTraty, String opisanie, String dohod, String arenda, String prorab, String comment, String shortName, int rowInFile) {
        this.date = date;
        this.chIN = chIN;
        this.chOut = chOut;
        this.sureName = sureName;
        this.hoursFacts = hoursFacts;
        this.hours = hours;
        this.priceForHour = priceForHour;
        this.profit = profit;
        this.kindOfPayment = kindOfPayment;
        this.customer = customer;
        this.objectName = objectName;
        this.dtAmount = dtAmount;
        this.dtCost = dtCost;
        this.salary = salary;
        this.dopTraty = dopTraty;
        this.opisanie = opisanie;
        this.dohod = dohod;
        this.arenda = arenda;
        this.prorab = prorab;
        this.comment = comment;
        this.shortName = shortName;
        this.rowInFile = rowInFile;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getChIN() {
        return chIN;
    }

    public void setChIN(String chIN) {
        this.chIN = chIN;
    }

    public String getChOut() {
        return chOut;
    }

    public void setChOut(String chOut) {
        this.chOut = chOut;
    }

    public String getSureName() {
        return sureName;
    }

    public void setSureName(String sureName) {
        this.sureName = sureName;
    }

    public String getHoursFacts() {
        return hoursFacts;
    }

    public void setHoursFacts(String hoursFacts) {
        this.hoursFacts = hoursFacts;
    }

    public String getHours() {
        return hours;
    }

    public void setHours(String hours) {
        this.hours = hours;
    }

    public String getPriceForHour() {
        return priceForHour;
    }

    public void setPriceForHour(String priceForHour) {
        this.priceForHour = priceForHour;
    }

    public String getProfit() {
        return profit;
    }

    public void setProfit(String profit) {
        this.profit = profit;
    }

    public String getKindOfPayment() {
        return kindOfPayment;
    }

    public void setKindOfPayment(String kindOfPayment) {
        this.kindOfPayment = kindOfPayment;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getObjectName() {
        return objectName;
    }

    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }

    public String getDtAmount() {
        return dtAmount;
    }

    public void setDtAmount(String dtAmount) {
        this.dtAmount = dtAmount;
    }

    public String getDtCost() {
        return dtCost;
    }

    public void setDtCost(String dtCost) {
        this.dtCost = dtCost;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getDopTraty() {
        return dopTraty;
    }

    public void setDopTraty(String dopTraty) {
        this.dopTraty = dopTraty;
    }

    public String getOpisanie() {
        return opisanie;
    }

    public void setOpisanie(String opisanie) {
        this.opisanie = opisanie;
    }

    public String getDohod() {
        return dohod;
    }

    public void setDohod(String dohod) {
        this.dohod = dohod;
    }

    public String getArenda() {
        return arenda;
    }

    public void setArenda(String arenda) {
        this.arenda = arenda;
    }

    public String getProrab() {
        return prorab;
    }

    public void setProrab(String prorab) {
        this.prorab = prorab;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public int getRowInFile() {
        return rowInFile;
    }

    public void setRowInFile(int rowInFile) {
        this.rowInFile = rowInFile;
    }

    public Boolean getPaidFor() {
        return paidFor;
    }

    public void setPaidFor(Boolean paidFor) {
        this.paidFor = paidFor;
    }

    public boolean isEmpty() {
        if (!chIN.isEmpty() || !chOut.isEmpty() || !sureName.isEmpty() || (!hoursFacts.isEmpty() && !hoursFacts.equals("0.0")) || (!hours.isEmpty() && !hours.equals("0.0")) || (!priceForHour.isEmpty() && !priceForHour.equals("0.0")) || !customer.isEmpty() || !objectName.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public Object[] toRow() {
        return new Object[]{(new SimpleDateFormat("dd.MM.yyyy")).format(date.getTime()), shortName, chIN, chOut, sureName, hoursFacts, hours, priceForHour, profit, kindOfPayment, customer, objectName, dtAmount, !dtCost.isEmpty() ? String.valueOf(new BigDecimal(dtCost).setScale(2, RoundingMode.HALF_DOWN)) : "", salary, dopTraty, opisanie, dohod, arenda, prorab, comment.equals("") ? "" : "*"};
    }

}
